/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Network;

import Chiabai_total.main;
import heartnew.Card;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author ThuyHuynh
 */
public class ReadWrite extends Thread {

    ObjectInputStream inp;
    ObjectOutputStream out;
    int flag = 0;

    public ReadWrite(ObjectOutputStream out, ObjectInputStream inp) {
        this.inp = inp;
        this.out = out;

    }
    int iboard;
    main frm;
    Object objCard;

    public void ReadCardInhand() throws IOException, ClassNotFoundException {
        Object obj = inp.readObject();
        if (obj.equals("chiabai")) {
            objCard = inp.readObject();
            Object key = inp.readObject();


            frm.Reset();
            frm.getDr().setArr((ArrayList<Card>) objCard);
            
            frm.getDr().PlayCard();
            frm.setIdplay((int)key);
            if ((int) key == (int) frm.getIdplayer()) {
                JOptionPane.showMessageDialog(null, "duoc quyen danh bai");
                frm.setValueProgressBar();
            } else {
                JOptionPane.showMessageDialog(null, "chua den luot");
            }
        }
        if (obj.equals("danhquanbai")) {
            Object vt = inp.readObject();//vi tri danh quan bai 
            Object idplayer = inp.readObject();//id cua nguoi danh bai ra 
            Object card = inp.readObject();
            Object idplay = inp.readObject();
            frm.setIdplay((int)idplay);
            int temp = frm.getIdplayer();//id chinh no

            if (temp == ((int) idplayer + 1) % 4) {
                JOptionPane.showMessageDialog(null, "da den luot bat dau danh di ");
                frm.setValueProgressBar();
            } else {
                JOptionPane.showMessageDialog(null, "chua den luot");
            }

            JOptionPane.showMessageDialog(null, "vi tri danh ra" + idplayer);
            if (((temp + 1) % 4) == (int) idplayer) {
                JOptionPane.showMessageDialog(null, "Right");
                frm.getDr().pushCardRight((int) vt, (Card) card);
                return;
            }

//            temp=((int)idplayer+1)%4;
            if (((temp + 2) % 4) == (int) idplayer) {
                JOptionPane.showMessageDialog(null, "Top");
                frm.getDr().pushCardTop((int) vt, (Card) card);

                return;
            }

            JOptionPane.showMessageDialog(null, "Left");
            frm.getDr().pushCardLeft((int) vt, (Card) card);


        }
    }

    public void Read() throws ClassNotFoundException, IOException {
        // try {

        if (flag == 0) {
            Object op = inp.readObject();

            iboard = (int) inp.readObject();
            Object idplayer = inp.readObject();


            frm = new main((boolean) op, this, (int) iboard, (int) idplayer);
            frm.setVisible(true);
            frm.pack();


            flag = 1;

        } else {
            ReadCardInhand();
        }
        //} catch (IOException ex) {
        //   JOptionPane.showMessageDialog(null, "mất kết nối với máy chủ");
        //   System.exit(0);
        // } catch (ClassNotFoundException ex) {
        //     Logger.getLogger(MultiThread.ReadWrite.class.getName()).log(Level.SEVERE, null, ex);
        //  }

    }

    public void Write(Object ob) throws IOException {
        out.writeObject(ob);
        if (ob.equals("chiabai")) {

            out.writeObject(iboard);
            iboard++;
        } else {
        }
    }

    @Override
    public void run() {
        JOptionPane.showMessageDialog(null, "Kết nối thành công.");
        while (true) {
            try {
                Read();
            } catch (ClassNotFoundException ex) {
                JOptionPane.showMessageDialog(null, ex);
                return;
            } catch (IOException ex) {

                JOptionPane.showMessageDialog(null, ex);
                return;
            }
        }
    }
}
